package controllers

import (
	"slotGame/models"
	"slotGame/utils"
	"strconv"
	"strings"

	"github.com/astaxie/beego"
)

//  CoinController oprations for Coin
type GameController struct {
	beego.Controller
}

// GetTurntableResult...
// @Title GetTurntableResult
// @Description get game turntable result
// @Param token query string true "qkkncuq7vcpcir93ttf65fo7t3"
// @Param bet_details query string true "1,2,3,4,0,1,11,99"
// @Success 200 返回成功
// @Failure 400 请求参数错误或者没有数椐
// @router /get_turntable_result [get]
func (c *GameController) GetTurntableResult() {
	var sid_ string
	var user_coin_value float64
	var bets_arr []int
	var out utils.DataResponse

	defer func(out *utils.DataResponse) {
		c.Data["json"] = out
		c.ServeJSON()
	}(&out)

	if sid_ = c.GetString("token"); sid_ != "" {
		coin_num, err := GetUserCoin(sid_)
		if err != nil || coin_num < 0 {
			out = utils.Reponse(400, nil, "未取到用户金币", "coin_num错误")
			beego.Error("未能领取到用户金币", err, coin_num)
			return
		}
		user_coin_value = coin_num
	}
	if bets := c.GetString("bet_details"); bets != "" {
		tmp_arrs := strings.Split(bets, ",")
		if len(tmp_arrs) != 8 {
			out = utils.Reponse(400, nil, "bet_details 押注个数不正确", bets+" bet_details 押注个数不正确")
			return
		}
		limit := GetBetLimit()
		for _, v := range tmp_arrs {
			if v == "0" || v == "" {
				bets_arr = append(bets_arr, 0)
			} else {
				num, err := strconv.Atoi(v)
				if err != nil {
					out = utils.Reponse(400, nil, "bet_details 参数不正确", bets+" "+err.Error())
					return
				}
				if num < 0 || num > limit {
					out = utils.Reponse(400, nil, "bet_details 参数不正确", bets+" 押注超过限额")
					return
				}
				bets_arr = append(bets_arr, num)
			}
		}
	}

	if sid_ == "" || len(bets_arr) != 8 {
		out = utils.Reponse(400, nil, "缺少参数", "token_ 或 bet_details 缺少参数")
		return
	}
	beego.Debug("\nSid:", sid_, "\nUserMoney:", user_coin_value, "\nbets_arr:", bets_arr)

	// TODO 玩家 判断 金币是否 充足等等 后 扣除金币

	// TODO 系统抽水 、 奖池库 发放

	// TODO  按照 押注 和 奖池 等概率来判断并且计算后返回

	out = utils.Reponse(400, nil, "金币操作错误", "金币操作错误")
	return
}

// GetGuessResult...
// @Title GetGuessResult
// @Description get game guess result
// @Param token query string true "qkkncuq7vcpcir93ttf65fo7t3"
// @Param coin  query string true "40"
// @Param user_choice  query string true "1-小 2-大"
// @Success 200 返回成功
// @Failure 400 请求参数错误或者没有数椐
// @router /get_guess_result [get]
func (c *GameController) GetGuessResult() {
	var out utils.DataResponse
	defer func(out *utils.DataResponse) {
		c.Data["json"] = out
		c.ServeJSON()
	}(&out)

	var sid_ string
	var user_coin_value float64
	var bet_num int
	var err error
	var user_choice string

	if sid_ = c.GetString("token"); sid_ != "" {
		coin_num, err := GetUserCoin(sid_)
		if err != nil || coin_num < 0 {
			out = utils.Reponse(400, nil, "未取到用户金币", "coin_num错误")
			return
		}
		user_coin_value = coin_num
	}

	if coin := c.GetString("coin"); coin != "" {
		bet_num, err = strconv.Atoi(coin)
		if err != nil {
			out = utils.Reponse(400, nil, "coin 参数不正确", coin+" "+err.Error())
			return
		}
		if bet_num <= 0 {
			out = utils.Reponse(400, nil, "coin 参数不正确", coin+" 不能小于等于零")
			return
		}
	}
	if user_choice = c.GetString("user_choice"); user_choice != "" {
		if user_choice != "1" && user_choice != "2" {
			out = utils.Reponse(400, nil, "user_choice 参数不正确", "user_choice 参数不正确")
			return
		}
	}

	if user_choice == "" || bet_num <= 0 || sid_ == "" {
		out = utils.Reponse(400, nil, "缺少参数", "token_ 或 bet_details 缺少参数")
		return
	}

	beego.Debug("\nSid:", sid_, "\nUserMoney:", user_coin_value, "\nbet_num:", bet_num, "\nuser_choice:", user_choice)

	if user_coin_value < float64(bet_num) {
		out = utils.Reponse(400, nil, "金币不足", nil)
		return
	}

	value_ := models.AddMunisCoin(-float64(bet_num), sid_, "猜大小下注")
	if value_ == "fail" {
		out = utils.Reponse(400, nil, "金币操作错误", "金币操作错误")
		beego.Error("金币操作错误", bet_num, sid_)
		return
	}

	// TODO 系统抽水 、 奖池库 发放

	// TODO  猜大小

	out = utils.Reponse(400, nil, "金币操作错误", "金币操作错误")
	return
}
